package com.tpy.controller;

import com.tpy.pojo.User;
import com.tpy.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author 达瓦里氏
 * @create 2021-10-26 20:55
 */
@RestController
@CrossOrigin //用来解决跨域问题
public class UserController {

    @Autowired
    UserService userService;

 /*   @PostMapping("/test")
    public String test(@RequestBody User user){
        userService.addUser(user);
        return "OK";
    }
*/
    @RequestMapping("/")
    public String index(){
        return "index";
    }

    /*查询所有用户*/
    @GetMapping("/queryUserList")
    public List<User> queryUserList(){
        List<User> userList = userService.queryUserList();
        return userList;
    }

    /*通过用户ID查询用户*/
    @GetMapping("/queryUserById/{id}")
    public User queryUserById(@PathVariable("id") int id){
        User user = userService.queryUserById(id);
        return user;
    }

    /*添加用户*/
    @PostMapping("/addUser")
    public void addUser(@RequestBody User user) {
        userService.addUser(user);
    }


    /*更新用户*/
    @GetMapping("/updateUser")
    public int updateUser(User user){
        int i = userService.updateUser(user);
        return i;
    }

    /*删除用户*/
    @GetMapping("/deletedUserById/{id}")
    public void deletedUserById(@PathVariable("id") int id){
        int i = userService.deletedUserById(id);
    }
}
